# coding=utf-8
# 欠税公告实体解析
import os
import sys
sys.path.append('../')
sys.path.append('../../')
import requests

import traceback
import owx_conf
from i_entity_extractor.extractors.default.default_extractor import DefaultExtractor


class OwxExtractor(DefaultExtractor):
    def __init__(self, topic_info, log):
        DefaultExtractor.__init__(self, topic_info, log)

    def entity_extract(self, parse_info, extract_data):
        '''法院公告实体解析入口'''
        extract_info = parse_info.extract_info
        url = parse_info.base_info.url
        self.log.info("owx_start_parse\turl:%s\ttopic_id:%s" % (url, extract_info.topic_id))

        entity_data_list = self.format_extract_data(extract_data)

        return entity_data_list

    def format_extract_data(self, extract_data):
        '''实体解析抽取数据'''
        entity_data_list = []
        excel_urls = extract_data.get("excel_urls", [])
        html_table = extract_data.get("html_table", "")
        owing_tax = str(extract_data.get("owing_tax", "")).replace(',', '')
        city = extract_data.get("city", "")
        money_flag = False

        if html_table != "" and len(excel_urls) > 0:
            self.log.error(
                "excel_urls and html_table all exsit, system do not know read data from where, excels_urls[%s] html_table[%s] " % (
                    excel_urls, html_table))
            return entity_data_list

        if len(excel_urls) > 0:  # excel链接
            excel_data_list, money_flag = self.read_from_excel(excel_urls, city)
            entity_data_list = self.replace_field(excel_data_list, extract_data, money_flag)
        elif html_table != "":  # 网页表格
            tmp_file = 'tmp.xlsx'
            excel_data_list = self.parser_tool.excel_parser.net_table_parser(html_table, tmp_file)
            entity_data_list = self.replace_field(excel_data_list, extract_data, money_flag)
        elif owing_tax != "":  # 结构化数据
            entity_data_list = self.proccess_norm_data(extract_data)
        else:
            pass

        return entity_data_list

    def read_from_excel(self, excel_urls, city):
        '''通过excel_urls下载文件并从excel中读取内容'''
        excel_data_list = []
        if len(excel_urls) == 0:
            return excel_data_list

        url_agent = 'Mozilla/5.0 (X11; Linux i86_64) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
        headers = {'User-Agent': url_agent}
        current_path = os.getcwd()
        money_flag = False
        try:
            for url in excel_urls:
                file = url.split('/')[-1].strip()
                filename_list = url.split('/')[-1].split('.')
                filename = filename_list[0].strip() + "." + filename_list[-1].strip()

                if not os.path.exists(current_path + '/' + file):
                    r = requests.get(url.strip(), headers=headers, stream=True)
                    with open(filename, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=1024):
                            if chunk:
                                f.write(chunk)
                                f.flush()
                    self.log.info("download [%s] ok !" % filename)
                if city in [u'广东']:
                    excel_data_list, money_flag = self.parser_tool.excel_parser.parser(filename, keyword=u"纳税人识别码")
                else:
                    excel_data_list, money_flag = self.parser_tool.excel_parser.parser(filename)

            return excel_data_list, money_flag

        except:
            self.log.error("read from excel error, ret[%s]" % traceback.format_exc())
            return []

    def proccess_norm_data(self, extract_data):
        '''处理结构化数据'''
        taxpayer_name = extract_data.get("taxpayer_name", "")
        tax_item = extract_data.get("tax_item", "")
        owing_date = extract_data.get("owing_date", "")
        province = extract_data.get("province", "")
        tax_authority = extract_data.get("tax_authority", "")
        legal_man = extract_data.get("legalman", "")
        company = extract_data.get("company", "")
        bulletin_date = extract_data.get("bulletin_date", "")
        try:
            owing_tax = round(float(str(extract_data.get("owing_tax", 0)).replace(',', '')), 2)
        except:
            owing_tax = 0.0
            self.log.warning("owing_tax transport error, src_owing_tax:%s" % extract_data.get("owing_tax", 0))

        try:
            owing_tax_cur_period = round(float(extract_data.get("owing_tax_cur_period", 0).replace(',', '')), 2)
        except:
            owing_tax_cur_period = 0.0
            self.log.warning(
                "owing_tax transport error, src_owing_tax_cur_period:%s" % extract_data.get("owing_tax_cur_period", 0))

        taxpayer_id = extract_data.get("taxpayer_id", "")
        legal_man_id = extract_data.get("legal_man_id", "")
        ori_owing_date = extract_data.get("ori_owing_date", "")
        business_address = extract_data.get("business_address", "")

        if province == "":
            province = self.parser_tool.province_parser.get_province(extract_data.get("city", ""))

        entity_data = {
            "province": province,
            "tax_authority": tax_authority,
            "legal_man": legal_man,
            "company": company,
            "bulletin_date": bulletin_date,
            "taxpayer_name": taxpayer_name,
            "tax_item": tax_item,
            "owing_tax_cur_period": owing_tax_cur_period,
            "owing_tax": owing_tax,
            "taxpayer_id": taxpayer_id,
            "legal_man_id": legal_man_id,
            "owing_date": owing_date,
            "ori_owing_date": ori_owing_date,
            "business_address": business_address,
        }

        return [entity_data]

    def replace_field(self, data, extract_data, money_flag):
        '''替换中文字段为英文字段名'''
        entity_data_list = []
        tax_authority = extract_data.get("tax_authority", "")
        province = extract_data.get("province", "")
        bulletin_date = extract_data.get("bulletin_date", "")
        if province == "":
            province = self.parser_tool.province_parser.get_province(extract_data.get("city", ""))

        if len(data) > 0:
            for each in data:
                tmp_entity_data = {}
                entity_data = {}

                for key, value in each.items():
                    for key2, value2 in owx_conf.owx_extractor_map.items():
                        if key in value2:
                            if key.find('万元') != -1:
                                tmp_entity_data[key2] = float(value) * 10000
                            else:
                                tmp_entity_data[key2] = value

                        else:
                            pass

                try:
                    if money_flag:
                        owing_tax = round(float(tmp_entity_data.get("owing_tax", 0)), 2) * 10000
                    else:
                        owing_tax = round(float(tmp_entity_data.get("owing_tax", 0)), 2)
                except:
                    owing_tax = 0.0
                    self.log.warning("owing_tax transport error, src_owing_tax:%s" % extract_data.get("owing_tax", 0))

                try:
                    owing_tax_cur_period = round(float(extract_data.get("owing_tax_cur_period", 0)), 2)
                except:
                    owing_tax_cur_period = 0.0
                    self.log.warning("owing_tax transport error, src_owing_tax_cur_period:%s" % extract_data.get(
                        "owing_tax_cur_period", 0))

                taxpayer_name = tmp_entity_data.get("taxpayer_name", "")
                tax_item = tmp_entity_data.get("tax_item", "")
                owing_date = tmp_entity_data.get("owing_date", "")

                entity_data = {
                    "province": province,
                    "tax_authority": tax_authority,
                    "legal_man": tmp_entity_data.get("legal_man", ""),
                    "company": tmp_entity_data.get("company", ""),
                    "bulletin_date": bulletin_date,
                    "taxpayer_name": taxpayer_name,
                    "tax_item": tax_item,
                    "owing_tax_cur_period": owing_tax_cur_period,
                    "owing_tax": owing_tax,
                    "taxpayer_id": tmp_entity_data.get("taxpayer_id", ""),
                    "legal_man_id": tmp_entity_data.get("legal_man_id", ""),
                    "owing_date": owing_date,
                    "ori_owing_date": tmp_entity_data.get("ori_owing_date", ""),
                    "business_address": tmp_entity_data.get("business_address", ""),
                }

                entity_data_list.append(entity_data)

        return entity_data_list


if __name__ == "__main__":
    import conf
    from common_parser_lib.parser_tool import ParserTool
    from bdp.i_crawler.i_extractor.ttypes import BaseInfo, CrawlInfo, ExtractInfo, PageParseInfo
    from entity_extractor_route import EntityExtractorRoute

    topic_id = 35
    parser_tool = ParserTool(conf)
    route = EntityExtractorRoute(conf, parser_tool)
    topic_info = route.all_topics.get(topic_id, None)
    obj = OwxExtractor(conf.log, topic_info, parser_tool)

    excel_data_list = parser_tool.excel_parser.parser('test.xls')
    print excel_data_list

    '''with open('宝应.txt', 'r') as f:
        html_table = f.read()

    extract_data = {
        "html_table": html_table,
        "excel_urls": [],
        "city": "宝应",
        "tax_authority": "江苏国家税务局",
        "bulletin_date": "2012-09-08",

    }

    host = '101.201.196.82'
    port = 27017
    database = 'public_data'
    coll = 'enterprise_owing_tax_fujian_excel'
    client = MongoClient(host, port)
    db = client[database][coll]
    param = {}
    cursor = db.find(param).skip(1)
    num = 0

    for item in cursor:
        num += 1
        item["city"] = u'福建'
        src_url = item.get("src_url", "")
        data = json.dumps(item)
        extract_info = ExtractInfo(ex_status=2, extract_data=data)
        base_info = BaseInfo(url=src_url)
        parser_info = PageParseInfo(base_info=base_info, extract_info=extract_info)
        data_list = obj.entity_extract(parser_info, item)
        for data in data_list:
            for key, value in data.items():
                if isinstance(value, list):
                    for i in value:
                        print key, ":", i
                elif isinstance(value, dict):
                    for key2, value2 in value.items():
                        print key2, ":", value2
                else:
                    print key, ":", value

        if num > 2:
            break'''
